﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Portal.Utility;
using Portal.Web.Utility;
using Portal.Web.Model;
using Portal.Exam.Bussiness;
using Portal.Exam.Model;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class AddBestAnswer 
    {
        [AjaxPro.AjaxMethod]
        public static string GetHtml(string SiteId)
        {
            string MonthiId = (string)WebEnvironments.Request(SiteId, "MonthiId");
           // string BackUrl = "ExamQuestion.aspx";
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
      
            string SelectLevel =
                "<select id=drpSelectLevelQuestion class=drplist>\r\n" +
                "   <option value=\"0\">Mức độ dễ</option>\r\n"+
                "   <option value=\"1\">Mức độ trung bình</option>\r\n" +
                "   <option value=\"2\">Mức độ khó</option>\r\n" +
                "</select>\r\n";
            string RetHtml =
                " <script language=javascript> \r\n" +
                " function ClientSaveAddNewBestAnswerQuestion() \r\n" +
                " { \r\n" +
                "     SiteId='"+SiteId+"';\r\n"+
                "     oEditor = FCKeditorAPI.GetInstance('FCKeditorBody');\r\n" +
                "     Question= oEditor.GetXHTML( true ); \r\n" +
                //"     Subject = document.getElementById('txtSubject').value;\r\n"+
                "     MaxPoint=document.getElementById(\"txtPoint\").value; \r\n" +
                "     MonthiId=document.getElementById(\"txtMonthiId\").value; \r\n" +
                "     SecondTime = document.getElementById(\"txtSecondTime\").value; \r\n" +
                "     IsMemory = document.getElementById(\"chkIsMemory\").checked; \r\n" +
                "     SecondHidden = document.getElementById(\"txtSecondHidden\").value; \r\n" +
                "     SelectLevelIndex = document.getElementById(\"drpSelectLevelQuestion\").selectedIndex; \r\n" +
                "     SelectLevel = document.getElementById(\"drpSelectLevelQuestion\").options[SelectLevelIndex].value; \r\n" +
                "     QuestionId=Portal.Exam.Render.AddBestAnswer.ServerSideSaveAddNewBestAnswerQuestion(SiteId, MonthiId,Question,MaxPoint,SecondTime,IsMemory,SecondHidden, SelectLevel).value; \r\n" +
                "     if(QuestionId.indexOf(\"ERROR\")!=-1) \r\n" +
                "     { \r\n" +
                "         alert(QuestionId); \r\n" +
                "         return; \r\n" +
                "     } \r\n" +
                "     oEditor.SetHTML('');\r\n" +
                "     document.getElementById(\"txtPoint\").value=\"1\"; \r\n" +
                "     document.getElementById(\"txtSecondTime\").value=\"60\"; \r\n" +
                "     ShowClientUpdateUrlForm(QuestionId); \r\n" +
                " } \r\n" +


                " function ShowClientUpdateUrlForm(QuestionId) \r\n" +
                " { \r\n" +
                "     SiteId='" + SiteId + "';\r\n" +
                "     Url=Portal.Exam.Render.AddBestAnswer.GetShowUpdateForm(SiteId, QuestionId).value; \r\n" +
                "     if(Url.indexOf(\"ERROR\")!=-1) \r\n" +
                "     { \r\n" +
                "         alert(Url); \r\n" +
                "         return; \r\n" +
                "     } \r\n" +
                "     window.open(Url, '_self'); \r\n" +
                " } \r\n" +
                "</script>\r\n" +

            " <div class=DatabaseFunction>\r\n" +
            "        <ul>\r\n" +
            "            <li onclick=\"javascript:ClientSaveAddNewBestAnswerQuestion();\"><img class=image src=\"" + IconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
            "            <li onclick=\"javascript:window.open('examquestion.aspx','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
            "        </ul>\r\n" +
            "  </div>\r\n" +
            "   <h3>Thêm câu hỏi chọn một phương án trả lời</h3>\r\n"+
            "   <input type=hidden id=txtMonthiId value=\"" + MonthiId + "\">" +
            "   <div class=CssFormSaveUpdate>\r\n" +
            "      <table style=\"width:99%\">" +
            "          <tr>" +
            "              <td class=\"Col1\">Mức độ</td>" +
            "              <td>" + SelectLevel + "</td>" +
            "          </tr>" +
            //"          <tr>" +
            //"              <td class=\"Col1\">Tiêu đề:</td>" +
            //"              <td><input type=textbox class=textbox id=\"txtSubject\"></td>" +
            //"          </tr>" +
            "          <tr>" +
            "              <td class=Col1>Câu hỏi:</td>" +
            "              <td class=Col2>\r\n" +
            "               <textarea type=hidden id=\"FCKeditorBody\" name=\"FCKeditorBody\"></textarea>\r\n" +
            "               <script type=\"text/javascript\" src=\"/fckeditor/fckeditor.js\"></script>\r\n" +
            "               <script type=\"text/javascript\">\r\n" +
            "                   var oFCKeditor = new FCKeditor('FCKeditorBody');\r\n" +
            "                   oFCKeditor.BasePath = \"/fckeditor/\";\r\n" +
            "                   oFCKeditor.Height = \"600\";\r\n" +
            "                   oFCKeditor.ReplaceTextarea();\r\n" +
            "               </script>\r\n" +
            "             </td>\r\n" +
            "          </tr>" +
            "          <tr>" +
            "              <td class=\"Col1\">Điểm:</td>" +
            "              <td><input class=textbox id=txtPoint value=\"1\"></td>" +
            "          </tr>" +
            "          <tr>" +
            "              <td class=\"Col1\">Thời gian(giây):</td>" +
            "              <td><input class=textbox id=txtSecondTime value=\"30\"></td>" +
            "          </tr>" +
            "          <tr>" +
            "              <td class=\"Col1\">Câu hỏi trí nhớ:</td>" +
            "              <td><input class=checkbox type=checkbox id=chkIsMemory></td>" +
            "          </tr>" +
            "          <tr>" +
            "              <td class=\"Col1\">Thời gian ẩn:</td>" +
            "              <td><input class=textbox id=txtSecondHidden value=\"30\"></td>" +
            "          </tr>" +
            //"          <tr>" +
            //"              <td></td>" +
            //"              <td><input type=button class=button onclick=javascript:ClientSaveAddNewBestAnswerQuestion(); value=\"Ghi lại\"><input type=button class=button  onclick=\"javascript:window.history.back(-1);\" value=\"Quay lại\"></td>" +
            //"          </tr>" +
            "   </table>" +
            "</div>\r\n";
            
            return RetHtml;
        }

        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                string RetHtml = "<div id=divAddBestAnsContent>"+ GetHtml(SiteId)+"</div>";
                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage= ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideSaveAddNewBestAnswerQuestion(
            string SiteId, 
            string MonthiId, 
            string Question,
            string MaxPoint, 
            string SecondTime, 
            bool IsMemory, 
            string SecondHidden, 
            string SelectLevel)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();

                if (Question.Trim().Length == 0)
                {
                    throw new Exception("Nhập nội dung câu hỏi");
                }
                if (FunctionUtilities.checkInteger(MaxPoint) == false) throw new Exception("Dữ liệu điểm nhập không hợp lệ! Phải nhập kiểu số!");
                Question = Question.Trim();
                string QuestionId = new QuestionBll().AddBestAnswerQuestion(null, SiteId, "BestAnswer", MonthiId, Question, int.Parse(MaxPoint), int.Parse(SecondTime), IsMemory, int.Parse(SecondHidden), int.Parse(SelectLevel));
                return QuestionId;// ResourceBLL.GetText(WebPartId, "MessageAdddOk", "Thêm mới thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetShowUpdateForm(string SiteId, string QuestionId)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                QuestionCls QuestionCls = new QuestionBll().CreateModel(SiteId, QuestionId);

                string UpdateUrl = "";
                if (QuestionCls.Type.ToLower().Equals("yesno"))
                {
                    UpdateUrl = "UpdateYesNo.aspx?QuestionId="+QuestionId+"&type=YesNo";
                }
                if (QuestionCls.Type.ToLower().Equals("bestanswer"))
                {
                    UpdateUrl = "UpdateBestAnswer.aspx?QuestionId="+QuestionId+"&type=AddBestAnswer";
                }
                //if (QuestionCls.Type.ToLower().Equals("MultiChoice".ToLower()))
                //{
                //    UpdateUrl = Screen.BuildUrl(
                //        SiteId,
                //        new ParamCls[]{
                //        new ParamCls("WebPartId",new UpdateMultiChoice().WebPartId),
                //        new ParamCls("QuestionId",QuestionId)
                //    });
                //}
                //if (QuestionCls.Type.ToLower().Equals("FreeText".ToLower()))
                //{
                //    UpdateUrl = Screen.BuildUrl(
                //        SiteId,
                //        new ParamCls[]{
                //        new ParamCls("WebPartId",new UpdateFreeText().WebPartId),
                //        new ParamCls("QuestionId",QuestionId)
                //    });
                //}

                //if (QuestionCls.Type.ToLower().Equals("LargeFreeText".ToLower()))
                //{
                //    UpdateUrl = Screen.BuildUrl(
                //        SiteId,
                //        new ParamCls[]{
                //        new ParamCls("WebPartId",new UpdateFreeText().WebPartId),
                //        new ParamCls("QuestionId",QuestionId)
                //    });
                //}
                if (UpdateUrl.Trim().Length == 0)
                {
                    throw new Exception("Không cung cấp tính năng sửa chữa cho loại: " + QuestionCls.Type);
                }
                return UpdateUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}